﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.ComponentModel;
using System.Configuration;

namespace CYTS.Aoyou.Framework.Database
{
    /// <summary>
    /// sqlMap帮助类
    /// </summary>
    public class SqlMapHelper
    {


        public static string GetSqlMappingString(string sqlBusinesName, string sqlName)
        {
            string analyseSql = string.Empty;
            SqlMap unit = SqlMapFactory.GetSqlmap(sqlBusinesName);
            if (unit != null && unit.SqlMapConfigurationList != null && !string.IsNullOrEmpty(sqlName))
            {
                try
                {
                    SqlMapDetail detail = unit.SqlMapConfigurationList.Where(p => p.SqlName == sqlName).FirstOrDefault();
                    analyseSql = detail != null ? detail.OriginalSqlString : string.Empty;
                }
                catch (Exception ex)
                {
                    string msg = "解析sqlmap执行失败";
                    throw new Exception(msg, ex);
                }
            }
            return analyseSql;
        }



        public static object GetSqlMappingResultObject(string sqlBusinesName, string sqlName, params SqlParameter[] commandParameters)
        {
            SqlMap unit = SqlMapFactory.GetSqlmap(sqlBusinesName);
            return AnalyseSqlMapContentObj(unit, sqlName, commandParameters);
        }



        public static DataSet GetSqlMappingResultDataSet(string sqlBusinesName, string sqlName, params SqlParameter[] commandParameters)
        {
            SqlMap unit = SqlMapFactory.GetSqlmap(sqlBusinesName);
            return AnalyseSqlMapContent(unit, sqlName, commandParameters);
        }



        public static bool ExecuteSqlMapping(string sqlBusinesName, string sqlName, params SqlParameter[] commandParameters)
        {
            SqlMap unit = SqlMapFactory.GetSqlmap(sqlBusinesName);
            return AnalyseSqlMapContentVoid(unit, sqlName, commandParameters);
        }



        private static DataSet AnalyseSqlMapContent(SqlMap config, string sqlName, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlMapDetail detail = config.SqlMapConfigurationList.Where(p => p.SqlName == sqlName).FirstOrDefault();
                return ExecuteCollectionCommand(detail, commandParameters);
            }
            catch (Exception ex)
            {
                string msg = "解析sqlmap执行失败";
                throw new Exception(msg, ex);
            }
        }



        private static object AnalyseSqlMapContentObj(SqlMap config, string sqlName, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlMapDetail detail = config.SqlMapConfigurationList.Where(p => p.SqlName == sqlName).FirstOrDefault();
                return ExecuteScalarCommand(detail, commandParameters);
            }
            catch (Exception ex)
            {
                string msg = "解析sqlmap执行失败";
                throw new Exception(msg, ex);
            }
        }



        private static bool AnalyseSqlMapContentVoid(SqlMap config, string sqlName, params SqlParameter[] commandParameters)
        {
            try
            {
                bool successFlag = false;
                SqlMapDetail detail = config.SqlMapConfigurationList.Where(p => p.SqlName == sqlName).FirstOrDefault();
                int result = ExecuteNonQueryCommand(detail, commandParameters);
                if (result > 0)
                {
                    successFlag = true;
                }

                return successFlag;
            }
            catch (Exception ex)
            {
                string msg = "解析sqlmap执行失败";
                throw new Exception(msg, ex);
            }
        }



        private static string GetSqlServerConnectionString(SqlMapDetail detail)
        {
            DBProvider dbp = DataAdapterFactory.GetInstance().GetDBProviderByDataBaseType(DatabaseCategory.SqlServer);
            DBConnection dbConn = dbp.DBConnections.First(p => p.Name == detail.DBConnectionName);
            return dbConn.Connection;
        }



        private static object ExecuteScalarCommand(SqlMapDetail detail, params SqlParameter[] commandParameters)
        {
            string connStr = GetSqlServerConnectionString(detail);
            if (string.IsNullOrWhiteSpace(connStr))
            {
                throw new Exception("数据库连接串无效,请检查是否为空");
            }

            return SQLHelper.ExecuteScalar(connStr, detail.CommandType, detail.OriginalSqlString, commandParameters);
        }



        private static int ExecuteNonQueryCommand(SqlMapDetail detail, params SqlParameter[] commandParameters)
        {
            string connStr = GetSqlServerConnectionString(detail);
            if (string.IsNullOrWhiteSpace(connStr))
            {
                throw new Exception("数据库连接串无效,请检查是否为空");
            }

            return SQLHelper.ExecuteNonQuery(connStr, detail.CommandType, detail.OriginalSqlString, commandParameters);
        }



        private static DataSet ExecuteCollectionCommand(SqlMapDetail detail, params SqlParameter[] commandParameters)
        {
            string connStr = GetSqlServerConnectionString(detail);
            if (string.IsNullOrWhiteSpace(connStr))
            {
                throw new Exception("数据库连接串无效,请检查是否为空");
            }

            return SQLHelper.ExecuteDataset(connStr, detail.CommandType, detail.OriginalSqlString, commandParameters);
        }
    }
}


